package com.qaml.mes.device.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.earth.user.AuthSsoContext;
import com.earth.user.SsoLoginUser;
import com.qaml.mes.device.dao.CheckUpkeepRecordDO;
import com.qaml.mes.device.dao.mapper.CheckUpkeepRecordMapper;
import com.qaml.mes.device.dto.eqp.CheckUpkeepRecordListDTO;
import com.qaml.mes.device.enums.DeletedEnum;
import com.qaml.mes.device.repository.CheckUpkeepRecordRepository;
import com.qaml.mes.device.utils.DateUtils;
import com.qaml.mes.device.dto.eqp.CheckUpkeepRecordAppDTO;
import com.qaml.mes.device.dto.eqp.CheckUpkeepRecordDTO;
import com.qaml.mes.device.enums.CheckUpkeepTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName CheckUpkeepRecordRepositoryImpl
 * @Description TODO
 * @Author leikai
 * @Date 2024/2/27 0027 9:51
 * @Version 1.0
 */
@Repository
public class CheckUpkeepRecordRepositoryImpl implements CheckUpkeepRecordRepository {

    @Resource
    private CheckUpkeepRecordMapper checkUpkeepRecordMapper;

    @Override
    public void saveCheckUpkeepRecordDO(CheckUpkeepRecordDO checkUpkeepRecordDO) {
        checkUpkeepRecordMapper.insert(checkUpkeepRecordDO);
    }

    @Override
    public Long countByCheckUpkeepId(List<String> checkIds, String type, String status) {
        LambdaQueryWrapper<CheckUpkeepRecordDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(CheckUpkeepRecordDO::getCheckUpkeepId, checkIds);
        if (CheckUpkeepTypeEnum.CHECK.getType().equals(type) || CheckUpkeepTypeEnum.UPKEEP.getType().equals(type)) {
            lambdaQueryWrapper.eq(CheckUpkeepRecordDO::getType, type);
            lambdaQueryWrapper.ne(CheckUpkeepRecordDO::getStatus, status);
        } else {
            lambdaQueryWrapper.eq(CheckUpkeepRecordDO::getStatus, status);
        }
        lambdaQueryWrapper.eq(CheckUpkeepRecordDO::getIsDeleted, 0);
        return checkUpkeepRecordMapper.selectCount(lambdaQueryWrapper);
    }

    @Override
    public List<CheckUpkeepRecordDO> getByCheckUpkeepId(List<String> checkIds) {
        LambdaQueryWrapper<CheckUpkeepRecordDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(CheckUpkeepRecordDO::getCheckUpkeepId, checkIds);
        lambdaQueryWrapper.eq(CheckUpkeepRecordDO::getIsDeleted, 0);
        return checkUpkeepRecordMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public IPage<CheckUpkeepRecordDO> pageList(Integer page, Integer pageSize, CheckUpkeepRecordAppDTO recordAppDTO) {
        LambdaQueryWrapper<CheckUpkeepRecordDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckUpkeepRecordDO::getIsDeleted, 0);
        queryWrapper.eq(StringUtils.isNotBlank(recordAppDTO.getType()), CheckUpkeepRecordDO::getType, recordAppDTO.getType());
        queryWrapper.eq(StringUtils.isNotBlank(recordAppDTO.getEqpCode()), CheckUpkeepRecordDO::getEqpCode, recordAppDTO.getEqpCode());
        queryWrapper.eq(Objects.nonNull(recordAppDTO.getCheckUpkeepDate()), CheckUpkeepRecordDO::getCheckUpkeepDate, recordAppDTO.getCheckUpkeepDate());
        queryWrapper.ge(Objects.nonNull(recordAppDTO.getCheckUpkeepStartTime()), CheckUpkeepRecordDO::getCheckUpkeepStartTime, recordAppDTO.getCheckUpkeepStartTime());
        queryWrapper.le(Objects.nonNull(recordAppDTO.getCheckUpkeepEndTime()), CheckUpkeepRecordDO::getCheckUpkeepStartTime, recordAppDTO.getCheckUpkeepEndTime());
        queryWrapper.orderByDesc(CheckUpkeepRecordDO::getId);
        return checkUpkeepRecordMapper.selectPage(new Page<>(page, pageSize), queryWrapper);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public IPage<CheckUpkeepRecordDO> page(Integer page, Integer pageSize, CheckUpkeepRecordListDTO checkUpkeepRecordListDTO) {

        LambdaQueryWrapper<CheckUpkeepRecordDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CheckUpkeepRecordDO::getIsDeleted, 0);
        lambdaQueryWrapper
                .like(StringUtils.isNotBlank(checkUpkeepRecordListDTO.getEqpName()), CheckUpkeepRecordDO::getEqpName, checkUpkeepRecordListDTO.getEqpName())
                .eq(StringUtils.isNotBlank(checkUpkeepRecordListDTO.getEqpCode()), CheckUpkeepRecordDO::getEqpCode, checkUpkeepRecordListDTO.getEqpCode())
                .eq(StringUtils.isNotBlank(checkUpkeepRecordListDTO.getStatus()), CheckUpkeepRecordDO::getStatus, checkUpkeepRecordListDTO.getStatus())
                .eq(StringUtils.isNotBlank(checkUpkeepRecordListDTO.getType()), CheckUpkeepRecordDO::getType, checkUpkeepRecordListDTO.getType())
                .ge(Objects.nonNull(checkUpkeepRecordListDTO.getCheckUpkeepStartTime()), CheckUpkeepRecordDO::getCheckUpkeepStartTime, checkUpkeepRecordListDTO.getCheckUpkeepStartTime())
                .le(Objects.nonNull(checkUpkeepRecordListDTO.getCheckUpkeepEndTime()), CheckUpkeepRecordDO::getCheckUpkeepStartTime, checkUpkeepRecordListDTO.getCheckUpkeepEndTime())
                .orderByDesc(CheckUpkeepRecordDO::getId);
        return checkUpkeepRecordMapper.selectPage(new Page<>(page, pageSize), lambdaQueryWrapper);
    }

    @Override
    public void updateCheckUpkeepRecordArchive(String type, Date startDateByMonth, Date endDateByMonth) {
        SsoLoginUser ssoLoginUser = AuthSsoContext.getSsoLoginUser();
        checkUpkeepRecordMapper.updateCheckUpkeepRecordArchive(ssoLoginUser.getUserId(), ssoLoginUser.getUserName(), type, startDateByMonth, endDateByMonth);
    }

    @Override
    public List<CheckUpkeepRecordDO> selectCheckUpkeepStatus(Date startDateByMonth, Date endDateByMonth, String type) {
        LambdaQueryWrapper<CheckUpkeepRecordDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CheckUpkeepRecordDO::getIsDeleted, 0);
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(type), CheckUpkeepRecordDO::getType, type);
        lambdaQueryWrapper.ge(CheckUpkeepRecordDO::getCheckUpkeepStartTime, startDateByMonth);
        lambdaQueryWrapper.le(CheckUpkeepRecordDO::getCheckUpkeepEndTime, endDateByMonth);
        lambdaQueryWrapper.orderByAsc(CheckUpkeepRecordDO::getCheckUpkeepStartTime);
        return checkUpkeepRecordMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 根据设备编码和类型和检查日期查询任务详情
     *
     * @param eqpCode
     * @param type
     * @param checkUpkeepDate
     * @return
     */
    @Override
    public CheckUpkeepRecordDO getByEqpCodeAndTypeAndCheckUpkeepDate(String eqpCode, String type, Date checkUpkeepDate) {
        LambdaQueryWrapper<CheckUpkeepRecordDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (CheckUpkeepTypeEnum.CHECK.getType().equals(type)) {
            // 如果是按天查询，则只匹配当天开始时间
            Date startDate = DateUtils.getStartDate(checkUpkeepDate);
            // 使用这个时间进行精确匹配
            lambdaQueryWrapper.eq(CheckUpkeepRecordDO::getIsDeleted, 0);
            lambdaQueryWrapper.eq(CheckUpkeepRecordDO::getEqpCode, eqpCode);
            lambdaQueryWrapper.eq(CheckUpkeepRecordDO::getType, type);
            lambdaQueryWrapper.eq(CheckUpkeepRecordDO::getCheckUpkeepDate, startDate);
        } else if (CheckUpkeepTypeEnum.UPKEEP.getType().equals(type)) {
            // 如果是按周查询，则需要构造一周的开始和结束时间
            Date startDateOfWeek = DateUtils.getStartDate(DateUtils.getStartDateByWeek(checkUpkeepDate));
            Date endDateOfWeek = DateUtils.getEndDate(DateUtils.getEndDateByWeek(checkUpkeepDate));
            // 使用时间范围查询
            lambdaQueryWrapper.eq(CheckUpkeepRecordDO::getIsDeleted, DeletedEnum.NO.getCode());
            lambdaQueryWrapper.eq(CheckUpkeepRecordDO::getEqpCode, eqpCode);
            lambdaQueryWrapper.eq(CheckUpkeepRecordDO::getType, type);
            lambdaQueryWrapper.ge(CheckUpkeepRecordDO::getCheckUpkeepStartTime, startDateOfWeek);
            lambdaQueryWrapper.le(CheckUpkeepRecordDO::getCheckUpkeepEndTime, endDateOfWeek);
        }
        // 根据不同情况选择性执行查询
        return checkUpkeepRecordMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    public List<CheckUpkeepRecordDO> getByTypeAndCheckUpkeepDate(String type, Date startTime) {
        LambdaQueryWrapper<CheckUpkeepRecordDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CheckUpkeepRecordDO::getIsDeleted, 0);
        lambdaQueryWrapper.eq(CheckUpkeepRecordDO::getType, type);
        lambdaQueryWrapper.ge(CheckUpkeepRecordDO::getCheckUpkeepStartTime, startTime);
        return checkUpkeepRecordMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 给检保任务打上假期标记
     *
     * @param ids
     */
    @Override
    public void updateHolidayTipByIds(List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            return;
        }
        checkUpkeepRecordMapper.updateCheckUpkeepUserByIds(ids);
    }

    @Override
    public CheckUpkeepRecordDO getById(String id) {
        LambdaQueryWrapper<CheckUpkeepRecordDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CheckUpkeepRecordDO::getIsDeleted, 0);
        lambdaQueryWrapper.eq(CheckUpkeepRecordDO::getId, id);
        return checkUpkeepRecordMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    public void updateById(CheckUpkeepRecordDO checkUpkeepRecordDO) {
        checkUpkeepRecordMapper.updateById(checkUpkeepRecordDO);
    }
}